package service;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import entity.Book;
import entity.BookType;
import function.BookBaseDao2Impl;
import function.BookTypeBaseDao2Impl;
import function.Druid;
import interfaces.BookServiceDao;
import tool.MyTool;
import tool.TSUtility;
import view.LogView;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class BookServiceImpl implements BookServiceDao {
    Scanner sc=new Scanner(System.in);
    BookTypeBaseDao2Impl bookTypeBaseDao2=BookTypeBaseDao2Impl.getBookTypeBaseDao();
    BookBaseDao2Impl bookImpl=BookBaseDao2Impl.getBaseDao2();

    /**
     * 添加图书
     * @throws Exception
     */
    @Override
    public void insert() throws Exception {
        System.out.println("请输入图书名称(直接按确定退出添加)：");
        String name = TSUtility.readString(5,"");
        if(name.equals("")){
            System.out.println("退出成功");
            return;
        }
        System.out.println("请输入作者：");
        String author= TSUtility.readString(4,null);
        System.out.println("请输入出版社名称：");
        String publish = TSUtility.readString(7,null);
        System.out.println("请输入出版日期：（yyyy-mm-dd）:");
        Date publish_date = MyTool.getDate();
        System.out.println("请输入图书的总数量：");
        Integer number =MyTool.readInt();
        if(number==null){
            number=0;
        }
        System.out.println("请输入单价：");
        Integer unitPrice = MyTool.readInt();
        if(unitPrice==null){
            unitPrice=0;
        }
        System.out.println("请选择图书类型：");
        List<BookType> all = bookTypeBaseDao2.getAll(Druid.getConnection());
        MyTool.printArr(all);
        System.out.println("请选择序号：");
        Integer typeIndex = MyTool.getTypeIndex(all.size());
        int type_id=0;
        if(typeIndex==null){
            type_id=26;
        }else {
            type_id = all.get(typeIndex).getId();
        }
        String ISBN=MyTool.getISBN();
        System.out.println("是否添加（Y/N）？");
        char c = TSUtility.readConfirmSelection();
        if(c=='N'){
            System.out.println("退出成功");
            return;
        }

        Book book=new Book(ISBN,type_id,name,author,publish,publish_date,number,unitPrice);
        boolean insert = bookImpl.insert(Druid.getConnection(), book);
        if(insert){
            Journal.record(LogView.getOperator().getName(),"添加了书本",name);
            System.out.println("添加图书成功");
        }else {
            System.out.println("添加图书失败");
        }

    }

    @Override
    public void deleteByISBN() throws Exception {
        System.out.println("请输入你要删除书本的序号(直接按确定退出)；");
        List<Book> all = getAll();
        Integer index = MyTool.getTypeIndex(all.size());
        if(index==null){
            System.out.println("退出成功");
            return;
        }
        String ISBN = all.get(index).getISBN();
        System.out.println("是否删除（Y/N）？");
        char c = TSUtility.readConfirmSelection();
        if (c=='N'){
            return;
        }
        try {
            boolean b = bookImpl.deleteByISBN(Druid.getConnection(), ISBN);
            if(b){
                Journal.record(LogView.getOperator().getName(),"删除了书本",all.get(index).getBook_name());
                System.out.println("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void update(String ISBN) throws Exception {
        Book book = bookImpl.getBookByISBN(Druid.getConnection(), ISBN);
        System.out.println("-----修改-----");
        System.out.print("书名（"+book.getBook_name()+"):");
        String name = TSUtility.readString(4, book.getBook_name());
        System.out.print("作者（"+book.getAuthor()+"):");
        String author = TSUtility.readString(4, book.getAuthor());
        System.out.print("出版社（"+book.getPublish()+"):");
        String publish = TSUtility.readString(7, book.getPublish());
        System.out.print("出版日期（"+book.getPublish_date()+"):");
        Date publishDate = MyTool.getDate();
        if(publishDate==null){
            publishDate=book.getPublish_date();
        }
        System.out.print("书本数（"+book.getNumber()+"):");
        Integer number = MyTool.readInt();
        if(number==null){
            number=book.getNumber();
        }
        System.out.print("单价（"+book.getUnitPrice()+"):");
        Float price = MyTool.readFloat();
        if(price==null){
            price=book.getUnitPrice();
        }
        System.out.println("书本类型（"+bookTypeBaseDao2.getBookTypeById(Druid.getConnection(),book.getType_id()).getTypeName()+"):");
        List<BookType> all = bookTypeBaseDao2.getAll(Druid.getConnection());
        MyTool.printArr(all);
        Integer typeIndex = MyTool.getTypeIndex(all.size());
        int type_id=0;
        if(typeIndex==null){
            type_id=book.getType_id();
        }
        else {
            type_id=all.get(typeIndex).getId();
        }
        Book book1=new Book(ISBN,type_id,name,author,publish,publishDate,number,price);
        char c = TSUtility.readConfirmSelection();
        System.out.println("是否修改（Y/N）?");
        if(c=='N'){
            return;
        }
        boolean update = bookImpl.update(Druid.getConnection(), ISBN, book1);
        if(update){
            Journal.record(LogView.getOperator().getName(),"修改了书本",book.getBook_name(),"的信息");
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }

    }

    @Override
    public Book getBookByISBN() throws IOException {
        Book book=null;
        System.out.println("请输入你要查询书本的ISBN编号(按确定退出)");
        String ISBN = MyTool.inputISBN();
        if(ISBN.equals("")){
            System.out.println("退出成功");
            return null;
        }
        try {
             book = bookImpl.getBookByISBN(Druid.getConnection(), ISBN);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(book==null){
            System.out.println("没有相关结果");
        }
        Journal.record(LogView.getOperator().getName(),"通过ISBN查询了",book.getBook_name());
        MyTool.printOne(book);
        return book;
    }

    @Override
    public Book getBookTypeByName() throws IOException {
        System.out.println("请输入你要查找的书本名(直接按确定退出)：");
        Book book=null;
        String bookName = TSUtility.readString(6, "");
        if(bookName.equals("")){
            System.out.println("退出成功");
            return null;
        }
        try {
             book = bookImpl.getBookTypeByName(Druid.getConnection(), bookName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(book==null){
            System.out.println("没有相关结果");
        }
        Journal.record(LogView.getOperator().getName(),"通过书本名称查询了书本",bookName);
        MyTool.printOne(book);
        return book;
    }

    @Override
    public List<Book> getBookByType() throws IOException {
        System.out.println("请输入你要查找的书本类型编号(直接按确定退出):");
        Integer bookTypeId = MyTool.readInt();
        if(bookTypeId==null){
            System.out.println("退出成功");
            return null;
        }
        List<Book> bookByType=null;
        try {
            bookByType = bookImpl.getBookByType(Druid.getConnection(), bookTypeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(bookByType==null){
            System.out.println("没有相关结果");
        }
        Journal.record(LogView.getOperator().getName(),"查询了书本类型",bookByType.get(0).getBook_name(),"相关书籍");

        MyTool.printArr(bookByType);
        return bookByType;
    }

    @Override
    public List<Book> getBookByAuthor() throws Exception {
        System.out.println("请输入要查询的作者名字(直接按确定退出)：");
        String author = TSUtility.readString(4, "");
        if(author.equals("")){
            System.out.println("退出成功");
            return null;
        }
        List<Book> bookByAuthor = bookImpl.getBookByAuthor(Druid.getConnection(), author);
        if(bookByAuthor==null){
            System.out.println("没有相关结果");
            return null;
        }
        Journal.record(LogView.getOperator().getName(),"查询了作者",bookByAuthor.get(0).getAuthor(),"相关书籍");
        MyTool.printArr(bookByAuthor);
        return bookByAuthor;
    }

    @Override
    public List<Book> getBookByPublish() throws Exception {
        System.out.println("请输入出版社的名称(直接按确定退出)");
        String publish = TSUtility.readString(7, "");
        if(publish.equals("")){
            System.out.println("退出成功");
            return null;
        }
        List<Book> bookByPublish = bookImpl.getBookByPublish(Druid.getConnection(), publish);
        if(bookByPublish==null){
            System.out.println("没有相关结果");
            return null;
        }
        Journal.record(LogView.getOperator().getName(),"查询了出版社",bookByPublish.get(0).getPublish(),"相关书籍");
        MyTool.printArr(bookByPublish);
        return bookByPublish;
    }

    @Override
    public List<Book> getAll() throws Exception {
        System.out.println("-----书本-----");
        List<Book> all = bookImpl.getAll(Druid.getConnection());
        if(all==null){
            System.out.println("还没有书本信息，请先添加");
            return null;
        }
        Journal.record(LogView.getOperator().getName(),"查询了所有书本");
        MyTool.printArr(all);
        return all;
    }

    public void addNumber(String ISBN) throws Exception {
    System.out.println("要添加多少本？（0~99）");
        int number = TSUtility.readInt();
        Book book = bookImpl.getBookByISBN(Druid.getConnection(), ISBN);
        if(book!=null)
        book.setNumber(number);
        boolean update = bookImpl.update(Druid.getConnection(), book.getISBN(), book);
        if(update){
            System.out.println("添加成功");
            return;
        }
        System.out.println("添加失败");

    }






    public static void main(String[] args) throws Exception {
        BookServiceImpl bookService=new BookServiceImpl();
        while (true) {

        }
    }
}
